using System.Collections;
using System.Collections.Generic;
using PALib;
using UnityEngine;

public class ObjectPool
{
    public IObjectPool<T> InitPool<T>(string poolName, GameObject obj) where T : Component
    {
        var objectPool = TMLPoolManager.Ins.GetPool<IObjectPool<T>>(poolName);
        if (objectPool == null)
        {
            //创建
            objectPool = TMLPoolManager.Ins.GeneratePool<T>(poolName, PoolContainerType.List, () =>
            {
                return Object.Instantiate(obj).GetComponent<T>();
                //on create callback
                //在对象池中拿物体时如果对象池空了，则会执行此处回调
                //return Instantiate(obj).GetComponent<T>();
            }, (item) =>
            {
                //on get from pool callback
                //从对象池中拿出物体时会执行此回调
                item.gameObject.SetActive(true);
            }, (item) =>
            {
                //on release to pool callback
                //把对象回收时会执行此回调
                item.gameObject.SetActive(false);
            }, (item) =>
            {
                //on destroy callback
                //当对象池中达到容纳上限再回收物体时则会执行此回调
                Object.Destroy(item.gameObject);
            });
        }
        return objectPool;
    }
}
